package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.adapter.WalletCouponHistoryAdapter;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCouponHistoryContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.CouponBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletCouponHistoryPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletCouponHistoryActivity extends BaseToolbarActivity implements WalletCouponHistoryContract.View {
    RecyclerView mRv;
    LinearLayout mLlNone;
    @Inject
    WalletCouponHistoryPresenter mPresenter;
    private WalletCouponHistoryAdapter mAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        initId();
        initInfo();
        setTbTitle(getString(R.string.wallet_coupon_invalid));
//        registerPush();
    }

    private void registerPush() {
        Disposable disposable = RxBus.get().toFlowable(EventBean.class).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean bean) throws Exception {
                if (bean != null) {
                    if (bean.type == Constants.EVENT_TYPE_REFRESH) {
                        initInfo();
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }

    private void initInfo() {
        mPresenter.getCoupons();
    }


    private void initId() {
        mRv = getViewById(R.id.rv_wallet_coupon);
        mLlNone = getViewById(R.id.ll_wallet_coupon_none);
        LinearLayoutManager mLinearManager = new LinearLayoutManager(this);
        mLinearManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(mLinearManager);
    }


    @Override
    protected int setView() {
        return R.layout.activity_wallet_coupon;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    protected void onRefreshData() {
        initInfo();
    }

    @Override
    public void getCouponBack(CouponBean list) {

        if (list == null) {
            mLlNone.setVisibility(View.VISIBLE);
            refreshFinish();
            return;
        }

        if (mAdapter == null) {
            mAdapter = new WalletCouponHistoryAdapter(this, list.getList(), list.getDoc_url());
            mRv.setAdapter(mAdapter);
        } else {
            mAdapter.replaceData(list.getList(), list.getDoc_url());
        }

        mLlNone.setVisibility(list.getList().isEmpty() ? View.VISIBLE : View.GONE);
        refreshFinish();
    }

    @Override
    public void getCouponBackFail(String msg) {
        TipUtils.showToast(App.getInstance(), msg);
        mLlNone.setVisibility(View.VISIBLE);
        refreshFinish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter != null && mAdapter.isLoading()) mAdapter.loadMoreEnd();
        refreshFinish();
    }

}
